﻿using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using TianlalaHSB.Utils;
using TianlalaHSB.Views;

namespace TianlalaHSB;

/// <summary>
///     Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private const int GWL_STYLE = -16;
    private const int WS_MAXIMIZEBOX = 0x00010000;

    public MainWindow()
    {
        // 应用程序启动时检查状态
        var authenticatedFile = "TianlalaHSB.ini";
        if (File.Exists(authenticatedFile) &&
            File.ReadAllText(authenticatedFile) == NetworkHelper.GetAllActiveMacAddresses().First())
        {
            // 用户已登录，跳过登录界面
            InitializeComponent();
            Loaded += MainWindow_Loaded;
            contentFrame.Source = new Uri("Views/ConsoleView.xaml", UriKind.Relative);
            menuListBox.SelectionChanged += MenuListBox_SelectionChanged;
        }
        else
        {
            OpenLoginView();
        }
    }

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    /// <summary>
    ///     将ResizeMode=NoResize时，将不会显示最大化最小化，只有关闭按钮；如下图：
    ///     将ResizeMode=CanMinimize时，最大化按钮被禁用，但是还是会显示，不能按，最小化，关闭按钮正常显示；
    ///     将WindowStyle=None时，将不会显示出最大化，最小化，以及关闭按钮，周围的边框也不存在。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var hwnd = new WindowInteropHelper(this).Handle;

        var windowStyles = GetWindowLong(hwnd, GWL_STYLE);
        SetWindowLong(hwnd, GWL_STYLE, windowStyles & ~WS_MAXIMIZEBOX);
    }

    // 显示登录界面
    private void OpenLoginView()
    {
        var loginView = new LoginView();
        loginView.Show();
        Close();
    }

    private void MenuListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (menuListBox.SelectedItem != null)
        {
            // 根据选中的菜单项更新内容区域的内容
            // 例如，你可以创建不同的UserControl实例并设置给contentControl.Content
            // 或者使用数据绑定和ViewModel来更新内容

            var selectedPage = menuListBox.SelectedItem.ToString();
            selectedPage = selectedPage?.Substring(selectedPage.IndexOf(':') + 1).Trim();

            // 根据选中的菜单项加载对应的页面
            switch (selectedPage)
            {
                case "商户资金账户查询":
                    contentFrame.Source = new Uri("Views/MerchantAccountView.xaml", UriKind.Relative);
                    break;

                case "商户资金账户余额查询":
                    contentFrame.Source = new Uri("Views/MerchantAccountBalanceView.xaml", UriKind.Relative);
                    break;

                case "商户资金账户明细查询":
                    contentFrame.Source = new Uri("Views/MerchantAccountRecordListView.xaml", UriKind.Relative);
                    break;

                case "商户清分账户明细查询":
                    contentFrame.Source = new Uri("Views/MerchantAccountRefundListView.xaml", UriKind.Relative);
                    break;

                case "会员资金账户查询":
                    contentFrame.Source = new Uri("Views/AccountView.xaml", UriKind.Relative);
                    break;

                case "会员资金账户余额查询":
                    contentFrame.Source = new Uri("Views/AccountBalanceView.xaml", UriKind.Relative);
                    break;

                case "会员资金账户明细查询":
                    contentFrame.Source = new Uri("Views/AccountRecordListView.xaml", UriKind.Relative);
                    break;

                default:
                    contentFrame.Content = new TextBlock { Text = "未找到对应页面:" + selectedPage };
                    break;
            }
        }
    }


    private void ExitButton_Click(object sender, RoutedEventArgs e)
    {
        // 注销时删除状态文件
        if (File.Exists("TianlalaHSB.ini")) File.Delete("TianlalaHSB.ini");

        OpenLoginView();
    }

    // 关闭方式
    //Application.Current.Shutdown(0);//0,1,2
    //Application.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
    //Application.Current.ShutdownMode = (ShutdownMode) Enum.Parse(typeof(ShutdownMode), "1");
    // - OnLastWindowClose：当最后一个窗口关闭或者调用System.Windows.Application.Shutdown方法时，应用程序会关闭。
    // - OnMainWindowClose：当主窗口关闭或者调用System.Windows.Application.Shutdown方法时，应用程序会关闭。
    // - OnExplicitShutdown：只有当调用System.Windows.Application.Shutdown方法时，应用程序才会关闭。

    // 对应 Windows - Closing
    //private void Window_Closing(object sender, CancelEventArgs e)
    //{
    //    Console.WriteLine(@"Closing");
    //    var result = MessageBox.Show("确认关闭?", "关闭应用",
    //        MessageBoxButton.YesNo,
    //        MessageBoxImage.Question);
    //    e.Cancel = (result == MessageBoxResult.No);
    //}

    // 对应 Windows - Closed
    //private void Window_Closed(object sender, EventArgs e)
    //{
    //    Console.WriteLine(@"Closed");
    //}
}